package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.ContentCategory;
import com.jyy.prefabricated.service.ContentCategoryService;
import com.jyy.prefabricated.vo.ContentCategoryData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 内容分类表(ContentCategory)表控制层
 *
 * @author makejava
 * @since 2021-05-13 14:43:02
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class ContentCategoryController {
    /**
     * 服务对象
     */
    @Resource
    private ContentCategoryService contentCategoryService;


    /**
     * 新增内容分类
     *
     * @param contentCategory
     * @return
     */
    @PostMapping("/content-category")
    public AjaxResult insert(@Valid @RequestBody ContentCategory contentCategory) {
        if (contentCategory.getPid() == null) {
            contentCategory.setPid(0);
        }
        int id = contentCategoryService.insert(contentCategory);
        ContentCategory result = contentCategoryService.queryById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新内容分类
     *
     * @param contentCategory
     * @return
     */
    @PutMapping("/content-category")
    public AjaxResult update(@Valid @RequestBody ContentCategory contentCategory) {
        log.info("Rest request to update contentCategory:{}", contentCategory);
        contentCategoryService.update(contentCategory);
        ContentCategory result = contentCategoryService.queryById(contentCategory.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询内容分类详情
     *
     * @param id
     * @return
     */
    @GetMapping("/content-category/{id}")
    public AjaxResult selectById(@PathVariable Integer id) {
        log.info("Rest request to find contentCategory by id:{}", id);
        ContentCategory result = contentCategoryService.queryById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除内容分类
     *
     * @param id
     * @return
     */
    @DeleteMapping("/content-category/{id}")
    public AjaxResult deleteById(@PathVariable Integer id) {
        log.info("Rest request to delete contentCategory by id:{}", id);
        boolean row = contentCategoryService.deleteById(id);
        if (row == true) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 分页查询内容分类
     *
     * @param category 分类名称
     * @param pid      父级分类id
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/content-category")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String category, @RequestParam(required = false) Integer pid, Pageable pageable) {
        log.info("Rest request to find ContentCategory by category:{},pid:{}", category, pid);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ContentCategory> categories = contentCategoryService.selectByParams(category, pid);
        PageInfo<ContentCategory> result = new PageInfo<>(categories);
        return AjaxResult.success(result);
    }

    /**
     * 查询内容分类列表
     *
     * @param category 分类名称
     * @param pid      父级分类id
     * @return
     */
    @GetMapping("/content-category-list")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String category, @RequestParam(required = false) Integer pid) {
        log.info("Rest request to find ContentCategory by category:{},pid:{}", category, pid);
        List<ContentCategory> result = contentCategoryService.selectByParams(category, pid);
        return AjaxResult.success(result);
    }

    /**
     * 查询内容分类返回树形结构
     *
     * @param pid 父级分类id
     * @return
     */
    @GetMapping("/content-category-tree/{pid}")
    public AjaxResult selectByPId(@PathVariable Integer pid) {
        log.info("Rest request to find ContentCategory by pid:{}", pid);
        List<ContentCategoryData> result = contentCategoryService.selectTree(pid);
        return AjaxResult.success(result);
    }

}
